<template>
  <div>
    <el-row>
      <el-col>
        <el-form :inline="true" :model="formInline">
          <el-form-item>
            <el-input size="mini" v-model="formInline.user" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input size="mini" v-model="formInline.number" placeholder="请输入工号/学号"></el-input>
          </el-form-item>
          <el-form-item>
            <el-select clearable size="mini" v-model="formInline.status" placeholder="请选择状态">
              <el-option label="启用" value="1"></el-option>
              <el-option label="禁用" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="mini" @click="search">查询</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-row class="buttonList">
      <el-col>
       <!-- <el-button type="primary" size="mini">编辑</el-button>-->
        <el-button type="danger" size="mini">启用/禁用</el-button>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="4">
        <el-tree accordion highlight-current :data="data" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
      </el-col>
      <el-col :span="20">
        <el-table
                :data="tableData"
                border
                size="mini"
                @selection-change="handleSelectionChange"
                style="width: 100%">
          <el-table-column
                  align="center"
                  type="selection"
                  width="55">
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="NAME"
                  label="用户名">
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="NUM"
                  label="学号/工号">
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="SOURCENAME"
                  label="类型">
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="ROLENAME"
                  label="权限">
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="STATUS"
                  label="状态">
            <template slot-scope="scope">
              {{scope.row.STATUS ? '启用' : '禁用'}}
            </template>
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="CREATEUSERNAME"
                  label="创建人">
          </el-table-column>
        </el-table>
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                background
                layout="total, sizes, prev, pager, next, jumper"
                :page-size="pagination.pageSize"
                :total="pagination.total"
                :page-sizes="[10, 20, 30, 50, 100]"
        >
        </el-pagination>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  export default {
    name: "orgManagement",
    data() {
      return{
        formInline: {},
        pagination: {
          pageSize: 20,
          total: 1,
          pageNo: 1
        },
        data: [],
        tableData: [],
        defaultProps: {
          children: 'list',
          label: 'NAME'
        },
        nodeData: {},
        departmentId: '',
        majorId: '',
        gradeId: '',
        classesId: ''
      }
    },
    created() {
      this.getUser(this.pagination.pageSize, this.pagination.pageNo);
      this.getTree();
    },
    methods: {
      search() {
        this.$post('/user/findUser', {
          pageSize:this.pagination.pageSize,
          pageNo: 1,
          name: this.formInline.user,
          number: this.formInline.number,
          status: this.formInline.status,
          departmentId: this.departmentId,
          majorId: this.majorId,
          gradeId: this.gradeId,
          classesId: this.classesId,
          userType: 1
        }).then(res => {
          if(res.data.status != 1) {
            this.$message.error('条件输入错误，请检查');
            return;
          }
          this.pagination.total = res.data.data.total;
          this.tableData = res.data.data.dataList;
        })
      },
      getTree() {
        this.$post('/public/findStructure').then(res => {
          this.data = res.data.data;
        })
      },
      getUser(pageSize,pageNo) {
        this.$post('/user/findUser', {pageSize, pageNo, userType: 1}).then(res => {
          this.pagination.total = res.data.data.total;
          this.tableData = res.data.data.dataList;
        });
      },
      getRole() {
        this.$post('/role/findRole').then(res => {
          if(res.data.status != 1) {
            this.$message.error('还未创建角色，请先创建角色');
            return;
          } else {
            this.options = res.data.data.dataList;
            this.dialogFormVisible = true
          }
        })
      },
      roleAuth() {
        this.getRole();
      },
      handleSelectionChange(val) {
        this.selectId = val;
        if(val.length > 0) {
          this.btnDisabled = false;
        } else {
          this.btnDisabled = true;
        }
      },
      submitRole() {
        if (this.value.length === 0) {
          this.$message.error('角色为必选项，请选择');
          return;
        }
        let userId = [];
        for(let i = 0; i < this.selectId.length; i++) {
          userId.push(this.selectId[i].ID);
        }
        this.$post('/user/addRole', {
          userId: userId.join(),
          roleId: this.value.join()
        }).then(res => {
          if(res.data.status != 1) {
            this.$message.error('请求错误，请稍后再试');
            return;
          }
          this.getUser(this.pagination.pageSize, this.pagination.pageNo);
          this.dialogFormVisible = false;
        })
      },
      handleSizeChange(size) {
        this.pagination.pageSize = size;
        this.getUser(size, this.pagination.pageNo);
      },
      handleCurrentChange(page) {
        this.pagination.pageNo = page;
        this.getUser(this.pagination.pageSize, page);
      },
      handleNodeClick(data,node) {
        this.nodeData = node;
        this.departmentId = '';
        this.classesId = '';
        this.gradeId = '';
        this.majorId= '';
        if(this.nodeData.level === 1) {
          this.departmentId = this.nodeData.data.ID;
          this.search();
          return;
        }
        if(this.nodeData.level === 2) {
          this.majorId = this.nodeData.data.ID;
          this.search();
          return;
        }
        if(this.nodeData.level === 4) {
          this.classesId = this.nodeData.data.ID;
          this.gradeId = '';
          this.majorId= '';
          this.search();
          return;
        }
        if(this.nodeData.level === 3) {
          this.majorId = this.nodeData.parent.data.ID;
          this.gradeId = this.nodeData.data.ID;
          this.search();
        }
      }
    }
  }
</script>

<style scoped lang="scss">
</style>
